//
//  SemaphoreViewController.m
//  ObjcGoNew
//
//  Created by 邢伟新 on 2021/4/16.
//

#import "SemaphoreViewController.h"
#import <Masonry/Masonry.h>
#import "ApiServer.h"

@interface SemaphoreViewController ()

@end

@implementation SemaphoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"信号量";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *barrierButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [barrierButton setTitle:@"异步任务同步执行" forState:(UIControlStateNormal)];
    [barrierButton addTarget:self action:@selector(semaphoreSyem1) forControlEvents:(UIControlEventTouchUpInside)];
    barrierButton.backgroundColor =[UIColor purpleColor];
    barrierButton.tag = 1001;
    [self.view addSubview:barrierButton];
    [barrierButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(150);
    }];
    
    
    UIButton *maxButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [maxButton setTitle:@"设置最大并发数" forState:(UIControlStateNormal)];
    [maxButton addTarget:self action:@selector(setMaxThread) forControlEvents:(UIControlEventTouchUpInside)];
    maxButton.backgroundColor =[UIColor purpleColor];
    maxButton.tag = 1001;
    [self.view addSubview:maxButton];
    [maxButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(barrierButton.mas_bottom).offset(15);
    }];
    
    
    UIButton *netButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [netButton setTitle:@"网络请求" forState:(UIControlStateNormal)];
    [netButton addTarget:self action:@selector(netWork3) forControlEvents:(UIControlEventTouchUpInside)];
    netButton.backgroundColor =[UIColor purpleColor];
    netButton.tag = 1001;
    [self.view addSubview:netButton];
    [netButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(maxButton.mas_bottom).offset(15);
    }];
    
    
    UIButton *writeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [writeButton setTitle:@"写入数据" forState:(UIControlStateNormal)];
    [writeButton addTarget:self action:@selector(whiteDateToArray) forControlEvents:(UIControlEventTouchUpInside)];
    writeButton.backgroundColor =[UIColor purpleColor];
    writeButton.tag = 1001;
    [self.view addSubview:writeButton];
    [writeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(netButton.mas_bottom).offset(15);
    }];
    

    
}

//设置线程最大并发数
-(void)setMaxThread{
    
    dispatch_semaphore_t sem = dispatch_semaphore_create(5);
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    for (int i = 0; i < 15; i++) {
        dispatch_async(queue, ^{
           
            //信号量减一
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            //做事
            NSLog(@"%d-----%@",i,[NSThread currentThread]);
            [NSThread sleepForTimeInterval:8];
            dispatch_semaphore_signal(sem);
    
        });
        
    }
    
}


//异步执行任务转换为同步执行
-(void)semaphoreSyem{
    NSLog(@"begin------%@",[NSThread currentThread]);
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //创建信号量 创建信号量为0
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    __block int num = 0;
    
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@"1----%@",[NSThread currentThread]);
        num = 100;
        
        //执行完成后信号量为1,线程继续执行
        dispatch_semaphore_signal(semaphore);
        
    });
    
    //异步执行到这里,此时信号量为-1,不再继续执行
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    NSLog(@"2----%@,num----%d",[NSThread currentThread],num);
    
}

//异步任务+并发队列+同步执行,优点:可开启多个线程可能性(不一定开启多个线程),发挥多核特点
-(void)semaphoreSyem1{
    
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    
    
    dispatch_async(queue, ^{
       
        NSLog(@"1----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);
    });
    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    
    dispatch_async(queue, ^{
       
        NSLog(@"2----%@",[NSThread currentThread]);
       
        dispatch_semaphore_signal(sem);
    });
    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    dispatch_async(queue, ^{
   
        NSLog(@"3----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);
        
    });
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    
    dispatch_async(queue, ^{
     
        NSLog(@"4----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);
    });
    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    dispatch_async(queue, ^{
     
        NSLog(@"5----%@",[NSThread currentThread]);

        dispatch_semaphore_signal(sem);
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    dispatch_async(queue, ^{
       
        NSLog(@"6----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);

    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    dispatch_async(queue, ^{
       
        NSLog(@"7----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    dispatch_async(queue, ^{
       
        NSLog(@"8----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);
    });

    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);


    dispatch_async(queue, ^{
       
        NSLog(@"9----%@",[NSThread currentThread]);
        dispatch_semaphore_signal(sem);

    });

    
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
    
    NSLog(@"结束喽---%@",[NSThread currentThread]);
    

    
    
    
    
}




//网络请求 A,B,C
-(void)network{
    
//    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//
//    NSLog(@"1----%@",[NSThread currentThread]);
//
//    dispatch_async(queue, ^{
//
//        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
//
//            NSLog(@"2----%@",[NSThread currentThread]);
//
//        } failure:^(NSError * _Nonnull error) {
//
//        }];
//
//        NSLog(@"3----%@",[NSThread currentThread]);
//    });
//
//    dispatch_async(queue, ^{
//
//
//
//        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
//
//            NSLog(@"4----%@",[NSThread currentThread]);
//        } failure:^(NSError * _Nonnull error) {
//
//        }];
//
//        NSLog(@"5----%@",[NSThread currentThread]);
//    });
//
//    dispatch_async(queue, ^{
//
//        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
//
//            NSLog(@"6----%@",[NSThread currentThread]);
//
//        } failure:^(NSError * _Nonnull error) {
//
//        }];
//
//        NSLog(@"7----%@",[NSThread currentThread]);
//    });
    
    
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
        NSLog(@"1----%@",[NSThread currentThread]);
    
    
       
   
    
    dispatch_group_async(group, queue, ^{
    
            [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
    
                NSLog(@"2----%@",[NSThread currentThread]);
    
            } failure:^(NSError * _Nonnull error) {
    
            }];
    
            NSLog(@"3----%@",[NSThread currentThread]);
        });
    
    dispatch_group_async(group, queue, ^{
    
    
    
            [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
    
                NSLog(@"4----%@",[NSThread currentThread]);
            } failure:^(NSError * _Nonnull error) {
    
            }];
    
            NSLog(@"5----%@",[NSThread currentThread]);
        });
    
    dispatch_group_async(group, queue, ^{
    
            [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
    
                NSLog(@"6----%@",[NSThread currentThread]);
    
            } failure:^(NSError * _Nonnull error) {
    
            }];
    
            NSLog(@"7----%@",[NSThread currentThread]);
        });
    
    dispatch_group_notify(group, queue, ^{
        
        NSLog(@"通知操作-----%@",[NSThread currentThread]);
    });
    
    
    
}

//网络请求A依赖B
-(void)netWork1{
    
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    
    dispatch_queue_t queue = dispatch_queue_create("xwx",DISPATCH_QUEUE_CONCURRENT);
    
    //网络a
    dispatch_async(queue, ^{
        
       
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
            
            NSLog(@"1----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
            
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"2----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
        }];
        
    });
    
    dispatch_async(queue, ^{
       
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
            
            NSLog(@"3----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
            
        } failure:^(NSError * _Nonnull error) {
            NSLog(@"4----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
        }];
        
    });
    
    
    
}

// a b c 三个网络请求,三个同时完成刷新UI
-(void)netWork3{
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);
        
        NSLog(@"begin1----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end1----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
            
        } failure:^(NSError * _Nonnull error) {
            
            dispatch_semaphore_signal(sem);
            
            NSLog(@"end1----%@",[NSThread currentThread]);
        }];
        
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);
        
        NSLog(@"begin2----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end2----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
            
        } failure:^(NSError * _Nonnull error) {
            
            dispatch_semaphore_signal(sem);
            
            NSLog(@"end2----%@",[NSThread currentThread]);
        }];
        
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_async(group, queue, ^{
        //创建信号量
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);
        
        NSLog(@"begin3----%@",[NSThread currentThread]);
        
        [ApiServer GeiWanApiSuccess:^(NSDictionary * _Nonnull resultDic) {
        
            NSLog(@"end3----%@",[NSThread currentThread]);
            dispatch_semaphore_signal(sem);
            
        } failure:^(NSError * _Nonnull error) {
            
            dispatch_semaphore_signal(sem);
            
            NSLog(@"end3----%@",[NSThread currentThread]);
        }];
        
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"刷新UI---%@",[NSThread currentThread]);
        });
    });
    
    
    
    
    
}


-(void)whiteDateToArray{
    
    NSMutableArray *mutaArray = [NSMutableArray array];
    dispatch_queue_t queue = dispatch_queue_create("xwx", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_semaphore_t sem = dispatch_semaphore_create(1);
    
    for (int i = 0; i < 100; i++) {
        
        
        dispatch_async(queue, ^{
            
            
            dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
            
            [mutaArray addObject:[NSNumber numberWithInt:i]];
            NSLog(@"%d----%@",i,[NSThread currentThread]);
            
            dispatch_semaphore_signal(sem);
            
            
        });
        
      
        
    }
    
  
    NSLog(@"数组为%@",mutaArray);
    
    
    
    
}


@end
